<?php $this->headLink()
->appendStylesheet($this->Css('button.css'))
->appendStylesheet($this->js('css/jquery.alerts.css'))
?>
<?php $this->headScript()
->appendFile($this->js('plugins/jquery.form.js'))
->appendFile($this->js('plugins/jquery.blockUI.js'))
->appendFile($this->js('plugins/jquery.alerts.js'))
->appendFile($this->js('jquery-plugins/ajaxupload.3.0.js'))
->captureStart()?>
alldata = [];
$(document).ready(function(){
	$().ajaxStop($.unblockUI);
	// 定義 ajax from
	$("#setForm").ajaxForm({
				dataType:  'json',
				success: showResponse
	});

 	count_student();

	$("#student_sn").change(function(){
	  var sn = $("#student_sn").val();
	   //$.cookie('Student_Sn',null);
    	$.cookies.set('Student_Sn',sn);
    	setdata();
	});

	$("#student_character input[type='checkbox']").click(function(){
		$(this).parent().toggleClass('checked');
	});

	$("#save-button").click(function(){
		$('#main_body').block({message:'<h1><?php echo $this->translate('Data setting ..')?></h1>',  border: '3px solid #f00'});
		$("#setForm").submit();

	});

	function showResponse(res) {
		if (res.error) {
			var message = '';
			$.each(res.error,function(i,val){
					$("#"+i).addClass('ui-state-error');
					$.each(val,function(ii,vv){
						message += $("#"+i+"-label label").html()+': '+ vv+ "\n";
					});
			});
			jAlert(message, '<?php echo $this->translate('checking errors')?>');
		}
		else{
			$(".ui-state-error").removeClass('ui-state-error ');
    		var sn = $("#student_sn").val();
    		$.each(res,function(i,v){
    		alldata[sn][i]=v;
    		});

			// 判斷下一位
    		if ($("#next_student").attr('checked')){
			var ss = $("#student_sn option:selected").next();
			if (ss) {
				$(ss).attr('selected','selected');
				setdata();
			}
		}
		}
		 $('#main_body').unblock();
		//console.log(alldata[sn]);
	}

	// 上傳照片處理
		var upload_img = new AjaxUpload('#upload_img', {
			action: '<?php echo  $this->route('uploadphoto')?>',
			onSubmit : function(file , ext){
				if (! (ext && /^(jpg|png|jpeg)$/.test(ext))){
					// extension is not allowed
					alert('<?php echo $this->translate('error image type')?>');
					// cancel upload
					return false;
					}
					this.setData({'student_sn':$("#student_sn").val()});
			},

			name: 'photo',
			onComplete : function(file,response){
				var sn = $("#student_sn").val();
				 var temp = '<img src="<?php echo  $this->route('studentimg')?>/student_sn/'+sn+'/photo/'+response+'"/>';
				alldata[sn].photo_name = response;
				$("#photo").html(temp);

			}

		});



	function getallData() {
		var year_semester = $("#year_semester").val();
		var class_id = $("#class_id").val();
		$.getJSON("<?php echo $this->route('getall')?>",{year_semester:year_semester,class_id:class_id},
    		function(data){
    			//console.log(data);
    			if($(data).length) {
    				$("#student_sn").show();
    				alldata= data;
    				setdata();
    			}
    			else {
    				$("#student_sn").hide();
    				$("#setForm").attr("disabled", true);
    				alert('<?php echo $this->translate('none data')?>');
    			}
    		}
		);
	}

	function setdata(){
		$(".ui-state-error").removeClass('ui-state-error ');
		var sn = setStudent();

		$.each(alldata[sn], function(i,item){
					if ($("input[name="+i+"]").attr('type')=='radio'){
						$("#"+i+"-"+item).attr('checked','checked');
					}else {
					   if (item == null  || item=='0000-00-00')
					   	item = '';
						$("#"+i).val(item);
					}
		});
		$("#student_character input[type='checkbox']").attr('checked','').parent().removeClass('checked');
		$.each(alldata[sn]['stud_kind'],function(i,item){
			$("#c-"+item).attr('checked',true).parent().addClass('checked');
		});

		if (alldata[sn].photo_name)
			 temp = '<img src="<?php echo  $this->route('studentimg')?>/student_sn/'+sn+'/photo/'+alldata[sn].photo_name+'"/>';
		else
			temp = '<img src="<?php echo $this->baseUrl?>/images/no_image.jpg"   />';
		//alert(temp);
		$("#photo").html(temp);
	}

	function getdata(){
		var cc = $("#student_sn option").length;
		if (cc==0) {
			$("#student_sn").hide();
			alert('<?php echo $this->translate('none data')?>');
			$(".rightArea input").attr('disabled','disabled');
			return false;
		}
		var sn =$("#student_sn").val();
		if (!sn){
			$("#student_sn option:first").attr('selected','selected');
			sn =$("#student_sn").val();
		}
		var year_semester = $("#year_semester").val();

		$.getJSON('<?php echo $this->route('getdata')?>',{sn:sn,year_semester:year_semester},function(data){
				$.each(data.student_data, function(i,item){
					if ($("input[name="+i+"]").attr('type')=='radio'){
						$("#"+i+"-"+item).attr('checked','checked');
					}else {
						$("#"+i).val(item);
					}

				});
		});
	}


	getallData();
});
<?php $this->headScript()->captureEnd()?>
<style>
.ui-widget-content {font-size:13px}
</style>
<div id="main_body">
<div class="leftArea">

<?php echo $this->render('student.phtml')?>

<a id="save-button" class="fg-button ui-state-default fg-button-icon-left ui-corner-all" >
<span class="ui-icon ui-icon-disk" ></span>
<?php echo $this->translate('save')?>
</a>
<p style="clear:both">
<input type="checkbox" id="next_student"  value="1" ><label for="next_student"><?php echo $this->translate('next student')?></label>
</p>
</div>

<!---------------- right area------------------------->
<div class="rightArea">
<form action="<?php echo $this->route('set')?>" method="post"  id="setForm" >
<fieldset class="ui-widget ui-widget-content ui-corner-all" style="float:left">
<legend class="ui-widget ui-widget-header ui-corner-all"><?php echo $this->translate('main data')?></legend>

<?php echo $this->form->year?>
<?php echo $this->form->semester?>
<?php echo $this->form->number?>
<?php echo $this->form->name?>
<?php echo $this->form->english_name?>
<?php echo $this->form->sex?>
<?php echo $this->form->birthday?>
<?php echo $this->form->identification?>
<?php echo $this->form->identification_number?>
<?php echo $this->form->colony?>
<?php echo $this->form->born_place?>
<?php echo $this->form->blood?>
<?php echo $this->form->nationality?>

</fieldset>
<div style="float:left">
<fieldset class="ui-widget ui-widget-content ui-corner-all" >
<legend class="ui-widget ui-widget-header ui-corner-all" ><?php echo $this->translate('address')?></legend>
<?php echo $this->form->registered_address?>
<?php echo $this->form->address?>
<table><tr>
<td><?php echo $this->form->address_zip?></td>
<td><?php echo $this->form->phone_home?></td>
</tr>
<tr>
<td><?php echo $this->form->phone_registered?></td>
<td><?php echo $this->form->phone_mobile?></td>
</tr>
<tr><td><?php echo $this->form->address_move_in?></td>
<td></td>
</tr>
</table>
<p class="msg_head"><?php echo  $this->translate('registered address')?></p>
<div class="msg_body hide" id="register_address">
<div class="addr_item">
<?php echo $this->form->registered_county?>
<?php echo $this->form->registered_town?>
<?php echo $this->form->registered_village?>
<?php echo $this->form->registered_neighbor?>
<?php echo $this->form->registered_road?>
</div>
<div  class="addr_item">
<?php echo $this->form->registered_section?>
<?php echo $this->form->registered_lane?>
<?php echo $this->form->registered_alley?>
<?php echo $this->form->registered_no?>
</div>
<div  class="addr_item">
<?php echo $this->form->registered_extend?>
<?php echo $this->form->registered_floor?>
<?php echo $this->form->registered_floor_extend?>
<?php echo $this->form->registered_other?>
</div>
<div style="clear:both"></div>
</div>
</fieldset>
<fieldset  class="ui-widget ui-widget-content ui-corner-all" >
<legend class="ui-widget ui-widget-header ui-corner-all" ><?php echo $this->translate('student character')?></legend>
<div id="student_character" >
<table>
<tr>
<?php $i=0;?>
<?php foreach($this->stud_kind_arr as $id=>$val):?>
<td> <input type="checkbox"  name="stud_kind[]"  id="c-<?php echo $id?>" value="<?php echo $id?>" /><label for="c-<?php echo $id?>"><?php echo $val?></label></td>
<?php if ($i++ %3 == 2):?>
</tr><tr>
<?php endif?>
<?php endforeach?>
</tr>
</table>
</div>
</fieldset>
</div>
<fieldset class="ui-widget ui-widget-content ui-corner-all" >
<div id="photo" style="width:200px">
</div>
<div   style="margin:auto;text-align:center;">
<input type="button"  id="upload_img" value="<?php echo $this->translate('upload photo')?>" />
</div>
<p class="msg_head"><?php echo  $this->translate('special student')?></p>
<div class="msg_body" id="special_student">
<?php echo $this->form->class_property?>
<?php echo $this->form->special_kind?>
<?php echo $this->form->special_class_property?>
</div>
<p class="msg_head"><?php echo  $this->translate('admission information')?></p>
<div class="msg_body" id="qualification">
<?php echo $this->form->student_id?>
<?php echo $this->form->enroll_year?>
<?php echo $this->form->enroll_school?>
<?php echo $this->form->preschool_qualification?>
<?php echo $this->form->preschool_educational_id?>
<?php echo $this->form->preschool_name?>
<?php echo $this->form->elementary_school_qualification?>
<?php echo $this->form->elementary_school_educational_id?>
<?php echo $this->form->elementary_school_name?>
</div>

</fieldset>

<input type="hidden" id="sn" name="sn" value="" />
</form>

</div>
</div>
<div style="clear:both"></div>